//
//  WBStatuesCell.swift
//  weibo
//
//  Created by yanghong on 15/12/12.
//  Copyright © 2015年 杨鸿. All rights reserved.
//

import UIKit
import SnapKit

let WBStatusesCellMargin: CGFloat = 10
class WBStatusesCell: UITableViewCell {
    // toolBar 顶部的约束
    var toolBarTopConstraint: Constraint?
    
    var statusesViewModel:WBStatuedViewModel? {
        // `外界` 给 statusesViewModel 赋值的时候会调用didSet
        didSet{
            
            originalView.statusViewModel = statusesViewModel
            toolBar.statusViewModel = statusesViewModel
            
            toolBarTopConstraint?.uninstall()
            
            if statusesViewModel?.status?.retweeted_status != nil {
                
                retweetView.hidden = false
                
                retweetView.statusesViewModel = statusesViewModel
                
                
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                
            }else {
                
                retweetView.hidden = true
                
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }
            
        }
        
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置UI
    private func setupUI() {
        
        // backgroundColor = RandomColor()
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        // 加载控件
        contentView.addSubview(originalView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        //添加约束
        // 原创微博的约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        //转发微博的约束
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.equalTo(originalView)
            
        }
        //底部三个按钮的约束
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.width.equalTo(contentView)
            make.height.equalTo(35)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
    
    // MARK -  懒加载控件
    
    private lazy var originalView: WBStatusesOriginalView = WBStatusesOriginalView()
    
    private lazy var toolBar: WBStatusesToolBar = WBStatusesToolBar()
    
    private lazy var retweetView: WBStatusesRetweetView = WBStatusesRetweetView()
}
